﻿using LightController;
using System;
using System.Collections.Generic;
using System.Threading;

namespace NDK.Motion
{
    [Serializable]
    public class ImageLights
    {
        public class RES
        {
            public bool bRtn { get; set; }
            public string msg { get; set; }
        }
        public ImageLights()
        {
            ImageName = string.Empty;
            LightList = new List<Light>();
        }
        public ImageLights(string imageName, List<Light> lightList)
        {
            ImageName = imageName;
            foreach (var light in _lightList)
            {
                _lightList.Add(new Light(light));
            }
        }
        public ImageLights(ImageLights imageLights)
        {
            ImageName = imageLights.ImageName;
            _lightList = new List<Light>();
            foreach (var light in imageLights.LightList)
            {
                _lightList.Add(new Light(light));
            }
        }
        public string ImageName;
        private List<Light> _lightList;

        public List<Light> LightList
        {
            get
            {
                return _lightList;
            }

            set
            {
                _lightList = value;
            }
        }
        private static readonly object lightOpenLock = new object();
        public bool Open(int timeout = 100)
        {

            if (LightList == null)
            {
                return false;
            }

            bool ret = true;
            try
            {
                foreach (var Light in LightList)
                {
                    lock (lightOpenLock)
                    {
                        ret &= Light.ON();
                        //Thread.Sleep(20);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Thread.Sleep(50);
            return ret;
        }
        public bool OpenAll()
        {
            if (LightList == null)
            {
                return false;
            }

            bool ret = true;
            try
            {
                ret &= LightList[0].ON(LightList);
            }
            catch { }
            return ret;
        }

        public bool CloseAll()
        {
            if (LightList == null)
            {
                return false;
            }

            bool ret = true;
            try
            {
                ret &= LightList[0].OFF(LightList);
            }
            catch { }
            return ret;
        }
        public RES Open(bool isCheckValue, int timeout = 100)
        {
            RES res = new RES();
            if (LightList == null)
            {
                res.bRtn = false;
                res.msg = $"{ImageName} LightList为null";
                return res;
            }

            bool ret = true;
            try
            {
                foreach (var Light in LightList)
                {
                    lock (lightOpenLock)
                    {
                        ret &= Light.ON();
                        Thread.Sleep(20);
                        res.msg += $"打开光源 Controller:{ Light.LightControllerName},CH：{Light.Name}[{Light.Chanel}],Value{ Light.Value}！";
                        if (isCheckValue)
                        {
                            int valueTemp = -1;
                            bool retRead = Light.ReadValue(ref valueTemp);
                            if (!retRead || Light.Value != valueTemp)
                            {
                                res.msg += $"确认光源亮度失败 Value{ Light.Value} 实际亮度{valueTemp}！";
                                ret &= false;
                            }
                            else
                            {
                                res.msg += $"确认光源亮度成功 Value{ Light.Value}！";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            res.bRtn = ret;
            if (res.bRtn)
                res.msg += $"ok";
            else
                res.msg += $"fail";

            return res;
        }
        public bool Close(bool isCheckValue = false, int timeout = 100)
        {
            if (LightList == null)
                return false;
            bool ret = true;
            foreach (var Light in LightList)
            {
                lock (lightOpenLock)
                {
                    ret &= Light.OFF();
                    Thread.Sleep(20);
                    if (isCheckValue)
                    {
                        int valueTemp = -1;
                        bool retRead = Light.ReadValue(ref valueTemp);
                        if (!retRead || 0 != valueTemp)
                        {
                            ret &= false;
                        }
                    }
                }
            }

            return ret;
        }
    }
}
